import psycopg2
import json
sequence_json = [
    {"id": 1,
    "now_state":"start",
    "next_state":"选择稿件",
    "condition":"无",
    "parameter":"无参数"
     },
    {"id": 2,
    "now_state":"选择稿件",
    "next_state":"初审",
    "condition":"无",
    "parameter":"无参数"},
    {"id": 1,
    "now_state":"初审",
    "next_state":"发邮件",
    "condition":"无",
    "parameter":{"reviewer":"张三"}
     }
]


def update_parameters(sequence_json):
    conn = psycopg2.connect(
        dbname="postgres",
        user="postgres",
        password="Rm15127373235*",
        host="39.107.64.125",
        port="5432"
    )
    cursor = conn.cursor()

    for block in sequence_json:
        now_state = block["now_state"]

        param = block.get("parameter")
        if isinstance(param, dict):
            parameter_value = json.dumps(param, ensure_ascii=False)
            cursor.execute("SELECT 1 FROM parameter WHERE now_state = %s;", (now_state,))
            exists = cursor.fetchone()

            if exists:
                cursor.execute(
                    "UPDATE parameter SET param = %s WHERE now_state = %s;",
                    (parameter_value, now_state)
                )
            else:
                cursor.execute(
                    "INSERT INTO parameter (now_state, param) VALUES (%s, %s);",
                    (now_state, parameter_value)
                )

    conn.commit()
    cursor.close()
    conn.close()
#update_parameters(sequence_json)